//
//  DestinationViewController.m
//  LNA
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 www.lanou3g.com蓝鸥. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationCollectionViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import "DestinationCell.h"
#import "DestinationModel.h"
#import "CollectionViewCellViewController.h"
@interface DestinationViewController ()<UITableViewDataSource,UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain) UIView *headerView;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *hot_countryArr;// 热门城市数组
@property (nonatomic, retain) NSMutableArray *country;//下面的tableViewCell的城市数组
@property (nonatomic, retain) UICollectionView *collecionView;
@end

@implementation DestinationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark - 进入时 显示的id
    [self dataHander:@"10"];

#pragma mark - tableview
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[DestinationCell class] forCellReuseIdentifier:@"cell"];
    
    
#pragma mark - header
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 640)];
    self.tableView.tableHeaderView = _headerView;
    [_headerView release];
#pragma mark -  header上的uiview
    UIImageView *picView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width , 250)];
    picView.backgroundColor = [UIColor greenColor];
    picView.image = [UIImage imageNamed:@"ditu"];
    [self.headerView addSubview:picView];
    [picView release];
 #pragma mark -  七大洲下面的热门目的地
    UILabel *hotLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 260, 100, 30)];
    hotLabel.text = @"热门目的地";
    hotLabel.font = [UIFont systemFontOfSize: 18];
    [self.headerView addSubview:hotLabel];
    [hotLabel release];
#pragma mark -  collectionView 下面的label 其他目的地
    UILabel *otherLabel =  [[UILabel alloc] initWithFrame:CGRectMake(20, 600, 120, 30)];
    [self.headerView addSubview:otherLabel];
    otherLabel.text = @"其他目的地";
    otherLabel.font = [UIFont systemFontOfSize:17];
    [otherLabel release];
    
#pragma mark -  collectionView 下面的label 拼音首字母排序
    UILabel *pinyinLabel = [[UILabel alloc] initWithFrame:CGRectMake(260, 600, 120, 30)];
    [self.headerView addSubview:pinyinLabel];
    pinyinLabel.text = @"拼音首字母排序";
    pinyinLabel.font = [UIFont systemFontOfSize:15];
    pinyinLabel.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    [pinyinLabel release];
    
    
#pragma mark -  七大洲点击按钮 button
    UIButton *asia = [UIButton buttonWithType:UIButtonTypeCustom];
    asia.frame = CGRectMake(270, 20, 40, 40);
    asia.backgroundColor = [UIColor redColor];
    [asia addTarget:self action:@selector(Asia1) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:asia];
    UIButton *europe = [UIButton buttonWithType:UIButtonTypeCustom];
    europe.frame = CGRectMake(200, 30, 40, 40);
    europe.backgroundColor = [UIColor greenColor];
    [europe addTarget:self action:@selector(Europe) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:europe];
    
    UIButton *africa = [UIButton buttonWithType:UIButtonTypeCustom];
    africa.frame = CGRectMake(200, 150, 40, 40);
    africa.backgroundColor = [UIColor whiteColor];
    [africa addTarget:self action:@selector(Africa) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:africa];
    
    UIButton *americaN = [UIButton buttonWithType:UIButtonTypeCustom];
    americaN.frame = CGRectMake(40, 30, 40, 40);
    americaN.backgroundColor = [UIColor yellowColor];
    [americaN addTarget:self action:@selector(AmericaN) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:americaN];
    
    UIButton *americaS = [UIButton buttonWithType:UIButtonTypeCustom];
    americaS.frame = CGRectMake(100, 120, 40, 40);
    americaS.backgroundColor = [UIColor greenColor];
    [americaS addTarget:self action:@selector(AmericaS) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:americaS];
    
    UIButton *oceania = [UIButton buttonWithType:UIButtonTypeCustom];
    oceania.frame = CGRectMake(320, 150, 40, 40);
    oceania.backgroundColor = [UIColor blackColor];
    [oceania addTarget:self action:@selector(Oceania) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:oceania];
    
    UIButton *antarctica = [UIButton buttonWithType:UIButtonTypeCustom];
    antarctica.frame = CGRectMake(150, 210, 40, 40);
    antarctica.backgroundColor = [UIColor yellowColor];
    [antarctica addTarget:self action:@selector(Antarctica) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:antarctica];
    
    [self collectionView];
    

}
#pragma mark - 南极洲按钮触发方法
- (void)Antarctica
{
   
    [self dataHander:@"759"];
    
}
#pragma mark - 大洋洲按钮触发方法
- (void)Oceania
{
    [self dataHander:@"239"];
}
#pragma mark - 南美洲按钮触发方法
- (void)AmericaS
{
    [self dataHander:@"235"];
}
#pragma mark - 北美洲按钮触发方法
- (void)AmericaN
{
    [self dataHander:@"234"];
}
#pragma mark - 非洲按钮触发方法
- (void)Africa
{
    [self dataHander:@"76"];
    
    
}
#pragma mark - 欧洲按钮触发方法
- (void)Europe
{
    [self dataHander:@"12"];
    
}
#pragma mark - 亚洲按钮触发方法
- (void)Asia1
{
    
    [self dataHander:@"10"];
}
#pragma mark - 数据处理
- (void)dataHander:(NSString *)cid
{
   NSString *str = @"http://open.qyer.com/qyer/footprint/continent_list?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=20&lat=38.88241944084977&lon=121.5398896720327&page=1&track_app_channel=App%2520Store&track_app_version=6.8.3&track_device_info=iPhone7%2C2&track_deviceid=0C79B4F1-2C09-4E9A-805E-05836431AB22&track_os=ios%25209.2&v=1";
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    [sessionManager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        NSLog(@"%lld",downloadProgress.totalUnitCount);
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSArray *arr = responseObject[@"data"];
        self.hot_countryArr = [NSMutableArray array];
        for (NSDictionary *dic in arr) {
            //判断
            if ([cid isEqualToString:[dic[@"id"] stringValue]]) {
                self.hot_countryArr = [DestinationModel modelHanderWithArrar: dic[@"hot_country"]];

                self.country = [DestinationModel modelHanderWithArrar:dic[@"country"]];
            }
            
        }
        [_collecionView reloadData];
        [_tableView reloadData];
        
        

        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
        
    }];
    
}
#pragma mark - collectionView
- (void)collectionView
{
    
    // flowLayout 流式布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    // cell 大小
    layout.itemSize = CGSizeMake(345 / 2, 540 / 2);
    // 行列间距
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    // 边界
    layout.sectionInset = UIEdgeInsetsMake(5, 10, 10, 10);
    self.collecionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 285, self.view.frame.size.width, 300) collectionViewLayout:layout];
    
    _collecionView.delegate = self;
    _collecionView.dataSource = self;
    _collecionView.backgroundColor = [UIColor whiteColor];
    [_headerView addSubview:_collecionView];
    
    // 重用池
    [_collecionView registerClass:[DestinationCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_collecionView release];
    [layout release];
    
}
#pragma mark - collectionView 实现方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _hot_countryArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DestinationModel *m = _hot_countryArr[indexPath.item];
    DestinationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//   cell.backgroundColor = [UIColor redColor];
    cell.cnnameLabel.text = m.cnname;
    cell.cnnameLabel.font = [UIFont systemFontOfSize:13];
    cell.ennameLabel.text = m.enname;
    cell.ennameLabel.font = [UIFont systemFontOfSize:12];
    cell.countLabel.text = [m.count stringValue];
    cell.countLabel.font = [UIFont systemFontOfSize:14];
    cell.labelLabel.text = m.label;
    cell.labelLabel.font = [UIFont systemFontOfSize:13];
    cell.cnnameLabel.textColor = [UIColor whiteColor];
    cell.ennameLabel.textColor = [UIColor whiteColor];
    cell.countLabel.textColor = [UIColor whiteColor];
    cell.labelLabel.textColor = [UIColor whiteColor];
   
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:m.photo]];
    return cell;
}
#pragma mark - collectionCell 跳转方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CollectionViewCellViewController *collectionVC = [[CollectionViewCellViewController alloc] init];
    DestinationModel *d = _hot_countryArr[indexPath.item];
    collectionVC.cid = [d.cid stringValue];
    collectionVC.label = d.label;
    [self.navigationController pushViewController:collectionVC animated:YES];
    [collectionVC release];
}
#pragma mark - tableView 实现方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _country.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    DestinationModel *m = _country[indexPath.row];
    DestinationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [NSString stringWithFormat:@"%@  %@",m.cnname,m.enname];
    
    cell.textLabel.font = [UIFont systemFontOfSize:15];
//    cell.detailTextLabel.text = m.enname;
//    cell.enname.textColor = [UIColor colorWithRed:85 / 255.0 green:85 / 255.0 blue:85 / 255.0 alpha:1];
    return cell;
    
    
    
}
#pragma mark - tableViewCell 跳转方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    DestinationModel *m = _country[indexPath.row];
    CollectionViewCellViewController *tableVC = [[CollectionViewCellViewController alloc] init];

    tableVC.cid = [m.cid stringValue];
    [self.navigationController pushViewController:tableVC animated:YES];
    
    
    [tableVC release];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
